﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace HostConfig
{
    /// <summary>
    /// 任务配置管理类
    /// </summary>
    public sealed class TasksConfigManager
    {
        /// <summary>
        /// 保存配置的代理
        /// </summary>
        /// <param name="configurationSaveMode">保存配置的方式</param>
        private delegate void Callback(ConfigurationSaveMode configurationSaveMode);

        private static bool CreateTasksSection(out TaskConfigurationSection taskConfigurationSection, out Callback callback)
        {
            try
            {
                TaskConfigurationSection tcs;
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                TaskConfigurationSection taskSection = (TaskConfigurationSection)config.GetSection("Tasks");
                if (taskSection == null)
                {
                    tcs = new TaskConfigurationSection();
                    config.Sections.Add("Tasks", tcs);
                    taskSection = (TaskConfigurationSection)config.GetSection("Tasks");
                    config.Save(ConfigurationSaveMode.Full);
                    taskConfigurationSection = taskSection;
                    callback = new Callback(config.Save);
                    return true;
                }
                else
                {
                    taskConfigurationSection = taskSection;
                    callback = new Callback(config.Save);
                    return true;
                }
            }
            catch (ConfigurationErrorsException err)
            {
                Console.WriteLine(err.ToString());
                taskConfigurationSection = null;
                callback = null;
                return false;
            }
        }

        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="task">任务</param>
        /// <returns>添加结果</returns>
        public static bool AddTask(TaskElement task)
        {
            try
            {
                TaskConfigurationSection tcs;
                Callback callback;
                if (CreateTasksSection(out tcs, out callback))
                {
                    if (null == tcs.Tasks[task.Name])
                    {
                        tcs.Tasks.Add(task);
                        callback(ConfigurationSaveMode.Full);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (ConfigurationErrorsException err)
            {
                Console.WriteLine(err.ToString());
                return false;
            }
        }

        /// <summary>
        /// 移除任务
        /// </summary>
        /// <param name="taskName">任务名称</param>
        /// <returns>移除结果</returns>
        public static bool RemoveTask(string taskName)
        {
            try
            {
                TaskConfigurationSection tcs;
                Callback callback;
                if (CreateTasksSection(out tcs, out callback))
                {
                    if (null == tcs.Tasks[taskName])
                    {
                        tcs.Tasks.Remove(taskName);
                        callback(ConfigurationSaveMode.Full);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (ConfigurationErrorsException err)
            {
                Console.WriteLine(err.ToString());
                return false;
            }
        }

        /// <summary>
        /// 获得任务信息
        /// </summary>
        /// <param name="taskName">任务名称</param>
        /// <returns>任务信息</returns>
        public static TaskElement GetTask(string taskName)
        {
            try
            {
                TaskConfigurationSection tcs;
                Callback callback;
                if (CreateTasksSection(out tcs, out callback))
                {
                    return tcs.Tasks[taskName];
                }
                else
                {
                    return null;
                }
            }
            catch (ConfigurationErrorsException err)
            {
                Console.WriteLine(err.ToString());
                return null;
            }
        }

        /// <summary>
        /// 获得所有任务信息
        /// </summary>
        /// <returns>任务信息列表</returns>
        public static List<TaskElement> GetAllTasks()
        {
            try
            {
                TaskConfigurationSection tcs;
                Callback callback;
                if (CreateTasksSection(out tcs, out callback))
                {
                    List<TaskElement> ts = new List<TaskElement>();
                    foreach (TaskElement t in tcs.Tasks)
                    {
                        ts.Add(t);
                    }
                    return ts;
                }
                else
                {
                    return null;
                }
            }
            catch (ConfigurationErrorsException err)
            {
                Console.WriteLine(err.ToString());
                return null;
            }
        }

        /// <summary>
        /// 根据任务名称匹配更新任务
        /// </summary>
        /// <param name="task">任务</param>
        /// <returns>如果任务不存在则返回false</returns>
        public static bool UpdateTask(TaskElement task)
        {
            try
            {
                TaskConfigurationSection tcs;
                Callback callback;
                if (CreateTasksSection(out tcs, out callback))
                {
                    if (null == tcs.Tasks[task.Name])
                    {
                        return false;
                    }
                    else
                    {
                        if (RemoveTask(task.Name))
                        {
                            return AddTask(task);
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (ConfigurationErrorsException err)
            {
                Console.WriteLine(err.ToString());
                return false;
            }
        }

        /// <summary>
        /// 移除所有任务
        /// </summary>
        /// <returns>移除结果</returns>
        public static bool RemoveAllTasks()
        {
            try
            {
                TaskConfigurationSection tcs;
                Callback callback;
                if (CreateTasksSection(out tcs, out callback))
                {
                    tcs.Tasks.Clear();
                    callback(ConfigurationSaveMode.Full);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (ConfigurationErrorsException err)
            {
                Console.WriteLine(err.ToString());
                return false;
            }
        }
    }
}
